﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Proggy.Web.Cms.Controllers.FrontEnd;
using System.Web.Mvc;
using Proggy.Web.Cms.Core.ContentModels;
using System.IO;
using System.Web;
using Proggy.Web.Cms.Cache;

namespace Proggy.Web.Cms.Core.Controllers.ContentModel
{
    /// <summary>
    /// Controller for ProggyFileContentModel
    /// </summary>
    public class ProggyContainerRedirectController : FrontEndControllerBase
    {
        public override ActionResult HandleUrl(int nodeId, bool asJson = false)
        {
            try
            {
                // get node
                var node = (ProggyContainerRedirectContentModel)this.CurrentContentModel;

                //this.PublishedContentRepository.GetTypedContentSingleFromId<ProggyFileContentModel>(nodeId);

                // if
                if (node.MustRedirect.SavedValue)
                {
                    if (node.RedirectUrlOrContent.AddedUrl.UrlType == Models.AddedUrlType.External)
                    {
                        return RedirectPermanent(node.RedirectUrlOrContent.AddedUrl.ExternalUrl);
                    }
                    else
                    {
                        var redirectNode = node.PublishedContentRepository.GetAbstractContentSingleFromId((int)node.RedirectUrlOrContent.AddedUrl.NodeId);
                        return RedirectPermanent(redirectNode.Url);
                    }

                }
                else {
                    throw new HttpException(404, "Page not found");
                }
            }
            catch
            {
                throw new HttpException(404, "Page not found");
            }
        }
    }
}
